package com.dsbridge.enrollment.web;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.dsbridge.enrollment.business.CourseBusinessService;
import com.dsbridge.enrollment.entities.CourseAuthor;
import com.dsbridge.enrollment.entities.Subject;

@ManagedBean(name="couseBean")
@RequestScoped
public class CourseController {

	private String courseName;
	private String courseDesc;
	private int subjectId;
	public String getCourseName() {
		return courseName;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}

	public String getCourseDesc() {
		return courseDesc;
	}

	public void setCourseDesc(String courseDesc) {
		this.courseDesc = courseDesc;
	}

	public int getSubjectId() {
		return subjectId;
	}

	public void setSubjectId(int subjectId) {
		this.subjectId = subjectId;
	}

	public int getAuthorId() {
		return authorId;
	}

	public void setAuthorId(int authorId) {
		this.authorId = authorId;
	}

	private int authorId;
	
    public CourseController(){
    	authorList=new ArrayList<SelectItem>();
    	subjectList=new ArrayList<SelectItem>();
    	ApplicationContext appContext=FacesContextUtils.getRequiredWebApplicationContext(FacesContext.getCurrentInstance());
    	CourseBusinessService courseBusinessService=appContext.getBean(CourseBusinessService.class);
    	List<CourseAuthor> courseAuthoirList=courseBusinessService.getAuthors();
    	List<Subject> subjectsList=courseBusinessService.getSubjects();
    	populateAuthorList(authorList,courseAuthoirList);
    	populateSubjectList(subjectList,subjectsList);
    	//selectItem.setLabel("Male");
    	//selectItem.setValue("M");
    	
    }
    
    private void populateSubjectList(List<SelectItem> subjectList2,
			List<Subject> subjectLuist) {
    	for (Subject courseAuthor : subjectLuist) {
			SelectItem selectItem=new SelectItem(courseAuthor.getSubjectId(), courseAuthor.getSubjectName());
			subjectList2.add(selectItem);
		}
				
	}

	private void populateAuthorList(List<SelectItem> authorList2,
			List<CourseAuthor> courseAuthoirList) {
		for (CourseAuthor courseAuthor : courseAuthoirList) {
			SelectItem selectItem=new SelectItem(courseAuthor.getAuthorID(), courseAuthor.getAuthor());
			authorList2.add(selectItem);
		}

		
	}

	private List<SelectItem> authorList;
    private List<SelectItem> subjectList;
    
    public List<SelectItem> getSubjectList() {
		return subjectList;
	}

	public void setSubjectList(List<SelectItem> subjectList) {
		this.subjectList = subjectList;
	}

	public List<SelectItem> getAuthorList() {
		return authorList;
	}

	public void setAuthorList(List<SelectItem> authorList) {
		this.authorList = authorList;
	}

	public String createCourse(){
    	    	ApplicationContext appContext=FacesContextUtils.getRequiredWebApplicationContext(FacesContext.getCurrentInstance());
    	CourseBusinessService courseBusinessService=appContext.getBean(CourseBusinessService.class);
    	return "";
    }

	
    
}
